import { login, qiyeWechatLoginVerify, logout, getInfo, getUserRoles, menuList,loginByMobile } from 'api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { getAppCode, setAppCode, removeAppCode } from '@/utils/apply'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    userRoles: [],
    menus: [],
    appCode: getAppCode() || '002',
    userInfo: {
      realName: '',
      head: require('assets/default-head.gif')
    }
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_USER_INFO: (state, user) => {
    state.userInfo = user
  },
  SET_USER_ROLES: (state, userRoles) => {
    state.userRoles = userRoles
  },
  SET_MY_MENU: (state, menus) => {
    state.menus = menus
  },
  SET_APP_CODE: (state, appCode) => {
    state.appCode = appCode
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { account, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ account: account.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data)
        setToken(data)
        resolve()
      }).catch(error => {
        console.log(error, 'error')
        reject(error)
      })
    })
  },

  // qywx login Verify
  qiyeWechat({ commit }, params) {
    return new Promise((resolve, reject) => {
      qiyeWechatLoginVerify(params).then(response => {
        const { data } = response
        commit('SET_TOKEN', data)
        setToken(data)
        resolve()
      }).catch(error => {
        console.log(error, 'error')
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response
        if (!data) {
          reject('获取用户信息失败，请重新登录')
        }
        commit('SET_USER_INFO', data)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user roles
  getUserRoles({ commit, state }) {
    return new Promise((resolve, reject) => {
      getUserRoles({
        userId: state.userInfo.id
      }).then(response => {
        const { data } = response
        if (!data) {
          reject('获取用户权限失败，请重新获取')
        }
        commit('SET_USER_ROLES', data)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user menu
  getMyMenu({ commit }) {
    console.log('get menu')
    return new Promise((resolve, reject) => {
      menuList().then(response => {
        const { data } = response
        if (!data) {
          reject('获取权限菜单失败，请重新获取')
        }
        commit('SET_MY_MENU', data)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  setAppCode({ commit }, data) {
    commit('SET_APP_CODE', data)
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  },

  // mobile login
  mobileLogin({ commit }, info) {
    return new Promise((resolve, reject) => {
      loginByMobile(info).then(response => {
        const { data } = response
        commit('SET_TOKEN', data)
        setToken(data)
        resolve()
      }).catch(error => {
        console.log(error, 'error')
        reject(error)
      })
    })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

